package com.mmall.controller.backend;

import com.mmall.common.Const;
import com.mmall.common.ResponseCode;
import com.mmall.common.ServerResponse;
import com.mmall.pojo.User;
import com.mmall.service.ICategoryService;
import com.mmall.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by chenxu on 17/7/26.
 */
@Controller
@RequestMapping(value = "/manage/category")
public class CategoryManageController {

    @Autowired
    IUserService userService;

    @Autowired
    ICategoryService categoryService;

    @RequestMapping(value = "add_category", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<Object> addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId",defaultValue="0")int parentId){
        // 检查是否管理员
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        // 检查是否为管理员
        Integer role = user.getRole().intValue();
        if(Const.Role.ROLE_ADMIN != role){
            return ServerResponse.createByErrorMessage("不是管理员");
        }

        // 处理类别service
        return categoryService.addCategory(categoryName,parentId);
    }

    @RequestMapping(value = "set_category.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse setCategoryName(HttpSession session, Integer categoryId, String categoryName){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        if (Const.Role.ROLE_ADMIN!= user.getRole().intValue()){
            return ServerResponse.createByErrorMessage("不是管理员");
        }
        return categoryService.updateCategoryName(categoryId,categoryName);
    }

    @RequestMapping(value = "get_category.do")
    @ResponseBody
    public ServerResponse getChildrenParallelCategory(HttpSession session, @RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        if (Const.Role.ROLE_ADMIN!= user.getRole().intValue()){
            return ServerResponse.createByErrorMessage("不是管理员");
        }

        // 查询子节点的category信息，并且不递归，保持平级
        return categoryService.getChildrenParallelCategory(categoryId);
    }


    @RequestMapping(value = "get_deep_category.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse getChildrenDeepCategory(HttpSession session, @RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录");
        }

        if (Const.Role.ROLE_ADMIN!= user.getRole().intValue()){
            return ServerResponse.createByErrorMessage("不是管理员");
        }

        // 查询子节点的category信息，并且不递归，保持平级
        return categoryService.selectCategoryAndChildrenById(categoryId);
    }
}